const http = require("http");
const fs = require("fs");
const path = require("path");
var imgPath = path.join(__dirname, "./icon.png");

http.createServer((req, res) => {
    var urlStr = req.url;
    /**当请求的资源路径是/时,相应网页文件到浏览器 */
    var filePath = path.join(__dirname, "./test.html");
    if (urlStr == "/") {
        var fileContent = fs.readFileSync(filePath);
        res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" });
        res.write(fileContent);
        res.end();
    } else if (urlStr == "/icon.png") {
        var imgContent = fs.readFileSync(imgPath);
        res.writeHead(200, { "Content-Type": "image/png;charset=utf-8" });
        res.write(imgContent);
        res.end();
    }
}).listen(8082);
console.log("server is listening on 8082");
